package com.ryzx.jpa.helper.base;


import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.ryzx.common.response.BapException;
import com.ryzx.common.response.BapResultCode;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

import java.io.Serializable;
import java.util.Objects;

@MappedSuperclass
@Getter
@Setter
public class BapBaseEntity implements Serializable {
    @Id()
    @GenericGenerator(name = "snowflakeId", type = GenerateSnowflakeId.class )
    @GeneratedValue(strategy = GenerationType.AUTO,generator = "snowflakeId")
    @JsonSerialize(using= ToStringSerializer.class)
    private Long id;

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof BapBaseEntity)
            return Objects.equals(id, ((BapBaseEntity) obj).id);
        else
            try {
                throw new BapException(BapResultCode.SERVER_ERROR);
            } catch (BapException e) {
                throw new RuntimeException(e);
            }
    }
}
